#ifndef _TIMER_H
#define _TIMER_H


#include "mutex.h"
#include <cstddef>
#include <iostream>
#include <memory>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

namespace droneswarm
{
    class DdapData;
    class TimerNode
    {
    public:
        TimerNode( std::shared_ptr< DdapData > requestData, int timeout );
        ~TimerNode();

        TimerNode( TimerNode &tn );
        void update( int timeout );
        bool isvalid();
        void clearReq();
        void setDeleted() { deleted_ = true; };
        bool isDeleted() const { return deleted_; }
        size_t getExpTime() const { return expiredTime_; }

    private:
        bool deleted_;
        size_t expiredTime_;
        std::shared_ptr< DdapData > SPHttpData;
    };
    struct TimerCmp
    {
        bool operator()( std::shared_ptr< TimerNode > &a, std::shared_ptr< TimerNode > &b ) const { return a->getExpTime() > b->getExpTime(); }
    };
    class TimerManager
    {
    public:
        TimerManager();
        ~TimerManager();
        void addTimer( std::shared_ptr< DdapData > SPHttpData, int timeout );
        void handleExpiredEvent();

    private:
        typedef std::shared_ptr< TimerNode > SPTimerNode;
        std::priority_queue< SPTimerNode, std::deque< SPTimerNode >, TimerCmp > timerNodeQueue;
    };

}  // namespace droneswarm

#endif